/** Part of Zombie Navigator
	Copyright © 2016  Zombie Navigator Developers
	
	@file
	@module
	@license AGPL-3.0+
	
*/ 'use strict';


/* Used modules */
const ENV_NAME_ENABLE_DEBUG = 'DEBUG', ENV_NAME_CONSOLE_VERBOSE = 'VERBOSE';
const ENV_NAME_DISABLE_ACTIVE_UI = 'DISABLE_ACTIVE_UI';
const ENV_NAME_DISABLE_APPS = 'DISABLE_APPS';

const {
	PREF_NAME_IGNORE_ENV
	, PREF_NAME_ALLOW_APPS
	, PREF_NAME_CONSOLE_LEVEL
	, PREF_NAME_DISABLE_UI
} = require('../pref-names.json');

const _prefs = require('./preferences').openPreferences([
	PREF_NAME_IGNORE_ENV
	, PREF_NAME_ALLOW_APPS
	, PREF_NAME_CONSOLE_LEVEL
	, PREF_NAME_DISABLE_UI
]);

const _pEnv = require('./env');


/* Private properties */
const LOG_ALL = 3, LOG_VERBOSE = 2, LOG_NORMAL = 1, LOG_SUPPRESSED = 0;
const _logLevel = level => {
	const pEnv = _pEnv;
	if (pEnv[ENV_NAME_CONSOLE_VERBOSE]) return LOG_VERBOSE;
	
	const prefs = _prefs;
	return prefs[PREF_NAME_CONSOLE_LEVEL];
};

/* Internal APIs */
const logAll = () => LOG_ALL <= _logLevel();
const logDetails = () => LOG_VERBOSE <= _logLevel();
const logImportant = () => LOG_NORMAL <= _logLevel();

// XXX: probably unused
const debugEnabled = () => {
	const pEnv = _pEnv;
	return !!pEnv[ENV_NAME_ENABLE_DEBUG];
};

const envEnabled = () => {
	const prefs = _prefs;
	return !prefs[PREF_NAME_IGNORE_ENV];
};

const appsAllowed = () => {
	const pEnv = _pEnv;
	const prefs = _prefs;
	
	if (pEnv[ENV_NAME_DISABLE_APPS]) return false;
	return !!prefs[PREF_NAME_ALLOW_APPS];
};

const activeUiEnabled = () => {
	const pEnv = _pEnv;
	const prefs = _prefs;
	
	if (pEnv[ENV_NAME_DISABLE_ACTIVE_UI]) return false;
	return !prefs[PREF_NAME_DISABLE_UI];
};


/* Exports */
exports.logAll = logAll;
exports.logDetails = logDetails;
exports.logImportant = logImportant;
exports.debugEnabled = debugEnabled;
exports.envEnabled = envEnabled;
exports.appsAllowed = appsAllowed;
exports.activeUiEnabled = activeUiEnabled;


// vim: ts=4 noet ai

